package leetcode;

import java.util.ArrayList;

/**
 * description:
 * author:张腾
 * date:2021-07-03
 */

/**
 * 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
 */
public class leetcode6 {
    public String convert(String s, int numRows) {
        if (numRows<2) return s;
        ArrayList<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            rows.add(new StringBuilder());
        }
        int i=0, flag=-1;
        for (char c : s.toCharArray()) {
            rows.get(i).append(c);
            if (i==0 || i==numRows-1) flag = -flag;
            i += flag;
        }
        StringBuilder sb = new StringBuilder();
        for (StringBuilder row : rows) {
            sb.append(row);
        }
        return sb.toString();
    }
}
